import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import login from '@/components/login'
import list from '@/components/list'
import buyCar from '@/components/buyCar'
import findBuyPage from '@/components/findBuyPage'
import axios from "axios";

Vue.use(Router)
// export default new
let rou =new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path:'/login',
      name:'login',
      component:login
    },
    {
      path:'/list',
      name:'list',
      component:list
    },
    {
      path:'/buyCar',
      name:'buyCar',
      component:buyCar
    },
    {
      path:'/findBuyPage',
      name:'findBuyPage',
      component:findBuyPage
    }
  ]
})
rou.beforeEach((to, from, next) => {
  if (to.path=='/'||to.path=='/login'){
    return next()
  }
  let token = window.sessionStorage.getItem('token');
  if (token){
    axios.defaults.headers.common['token']=token
    return next()
  }
  alert("请先登录")
  return next('/login')
})
export default rou
